<?php

class LanguagesInstall
{
	private $_config = array(
		'name' => 'languages',
		'title' => 'Language module',
		'dependencies' => array(
			'settings',
		),
		'import' => array(
			'application.modules.languages.models.Languages',
			'application.modules.languages.LanguagesModule',
		)
	);
	
	public function config()
	{
		return $this->_config;
	}

	public function install()
	{
		$sql = "DROP TABLE IF EXISTS {{languages}};";
		Yii::app()->db->createCommand($sql)->execute();

		$sql="
	CREATE TABLE IF NOT EXISTS {{languages}} (
	  `id` int(11) NOT NULL AUTO_INCREMENT,
	  `code` varchar(2) NOT NULL,
	  `name` varchar(16) NOT NULL,
	  PRIMARY KEY (`id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;";
		Yii::app()->db->createCommand($sql)->execute();

		$sql="INSERT INTO {{languages}} (`code`, `name`) VALUES (:code, :name);";
		$command = Yii::app()->db->createCommand($sql);

		$command->bindValue(":code", "ru", PDO::PARAM_STR);
		$command->bindValue(":name", "Русский", PDO::PARAM_STR);
		$command->execute();

		$command->bindValue(":code", "en", PDO::PARAM_STR);
		$command->bindValue(":name", "English", PDO::PARAM_STR);
		$command->execute();


		$sql="INSERT INTO {{settings}} (`category`, `key`, `value`) VALUES (:category, :key, :value);";
		$command = Yii::app()->db->createCommand($sql);

		$command->bindValue(":category", "localization", PDO::PARAM_STR);
		$command->bindValue(":key", "defaultLang", PDO::PARAM_STR);
		$command->bindValue(":value", serialize('en'), PDO::PARAM_STR);
		$command->execute();

		$command->bindValue(":category", "localization", PDO::PARAM_STR);
		$command->bindValue(":key", "adminDefaultLang", PDO::PARAM_STR);
		$command->bindValue(":value", serialize('ru'), PDO::PARAM_STR);
		$command->execute();
	}

	public function uninstall()
	{
		Yii::app()->db->createCommand()->dropTable('{{languages}}');
	}
}